package com.skyinno.mrms.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.springframework.util.ResourceUtils;

public class Config {
    private static File file;

    private static long time;

    public static Properties prop = new Properties();
    
    static {
        try {
            file = ResourceUtils.getFile("classpath:config.properties");
            reload();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void reload() throws Exception {
        synchronized (file) {
            if (file.lastModified() > time) {
                InputStream in = new FileInputStream(file);
                try {
                    prop.load(in);
                    time = file.lastModified();
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }
        }
    }
    public static String getProperty(String key) {
        if (file != null) {
            if (file.lastModified() > time) {
                try {
                    reload();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return prop.getProperty(key);
    }
}
